// var createError = require("http-errors");
// var express = require("express");
// const mongoose = require("mongoose");
// var path = require("path");
// var cookieParser = require("cookie-parser");
// var logger = require("morgan");
// // var hero = require("./routes/router");
// var indexRouter = require("./routes/index");
// var usersRouter = require("./routes/users");

// var app = express();


// //此处需要安装  post 请求解析的内容
// const bodyParser = require("body-parser");

// app.use(bodyParser.json());
// app.use(bodyParser.urlencoded({ extended: false }));
// //这里的myDbs是数据库的名字，不是表的名字
// // app.use('/api', mydb)


// // var mongoose = require('mongoose') // 引入 mongoose
// var url = "mongodb://localhost:27017/mydb?authSource=admin"; // 本地数据库地址
// mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, db) => {
//     if (err)
//         console.log("连接数据库失败" + err)
//     else
//         console.log("连接数据库成功")
// })

// // view engine setup
// app.set("views", path.join(__dirname, "views"));
// app.set("view engine", "hbs");

// app.use(logger("dev"));
// app.use(express.json());
// app.use(express.urlencoded({ extended: false }));
// app.use(cookieParser());
// app.use(express.static(path.join(__dirname, "public")));

// app.use("/", indexRouter);
// app.use("/users", usersRouter);

// // catch 404 and forward to error handler
// app.use(function(req, res, next) {
//     next(createError(404));
// });

// // error handler
// app.use(function(err, req, res, next) {
//     // set locals, only providing error in development
//     res.locals.message = err.message;
//     res.locals.error = req.app.get("env") === "development" ? err : {};

//     // render the error page
//     res.status(err.status || 500);
//     res.render("error");
// });
// app.listen(3001, () => {
//     console.log("app listening on port 3000.");
// });
// module.exports = app;

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var mongoose = require("mongoose");

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var hbs = require('hbs');
var app = express();

//此处需要安装  post 请求解析的内容
var bodyParser = require("body-parser");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//注册索引的helper +1
hbs.registerHelper("addOne", (index) => {
    return index + 1;
})

// 数据库
var  url  =  "mongodb://localhost:27017/mydb?authSource=admin";  // 本地数据库地址

mongoose.connect(url, { useNewUrlParser:  true,  useUnifiedTopology:  true }, (err, db) => {    
    if (err)        
        console.log("连接数据库失败" + err)    
    else        
        console.log("连接数据库成功")
})

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};

    // render the error page
    res.status(err.status || 500);
    res.render('error');
});

module.exports = app;